package aimis.service;

import aimis.domain.Monitor;
import aimis.dto.PageDTO;
import aimis.dto.PageQuery1;
import aimis.repo.MonitorMapper;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class MonitorServiceImpl extends ServiceImpl<MonitorMapper, Monitor> implements MonitorService {

    @Autowired
    private MonitorMapper monitorMapper;
    @Override
    public List<Monitor> getAll() {
        return monitorMapper.getAll();
    }

    @Override
    public List<Monitor> getByLikeName(String date) {
        return monitorMapper.getByLikeName(date);
    }

    @Override
    public boolean add(Monitor monitor) {
        return monitorMapper.add(monitor);
    }

    @Override
    public PageDTO<Monitor> queryUsersPage(PageQuery1 query) {
        //1.构建分页条件
        //1.1分页条件
        Page<Monitor> page=Page.of(query.getPageNo(), query.getPageSize());
        //1.2排序条件
        page.addOrder(new OrderItem("id",false));


        //2.分页查询
        //如果直接用page，需要创建page对象，还要querywrapper对象
        //直接用lambdaquery，可以直接加条件，最后来个page就直接分页了
        Page<Monitor> p=lambdaQuery()
                .page(page);

                //3.封装vo结果
        PageDTO<Monitor> dto=new PageDTO<>();
        //3.1总条数
        dto.setTotal(p.getTotal());
        //3.2总页数
        dto.setPages(p.getPages());
        //3.3当前页数据
        List<Monitor> records=p.getRecords();
        if(CollUtil.isEmpty(records)){
            dto.setList(Collections.emptyList());//返回一个空集合
        }
        dto.setList(records);
        //4.返回
        return dto;

    }
}
